﻿using Sasc.PV.BLL;
using Sasc.PV.Common;
using Sasc.PV.DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web
{
    [Flags]
    public enum GongXuQuanXianLeiXing
    {
        ADD = 1,
        MODIFY = 2,
        DELETE = 4,
        EXPORT = 8,
        /// <summary>
        /// 控制他人数据
        /// </summary>
        CONTROLOTHER = 16,
        FANGONG=32
    }

    public abstract class PVGongXuAdminPage<TCheJian, TGongXu, TGongXuTarget> : PVAdminPage
        where TGongXu : class, ICheJianGongXu<TCheJian, TGongXuTarget>, new()
        where TCheJian : class, ICheJianManagement, new()
        where TGongXuTarget : class, ICheJianGongXuTarget
    {

        #region Const
        protected const string QUANXIAN_ADD = "A";
        protected const string QUANXIAN_MODIFY = "M";
        protected const string QUANXIAN_DELETE = "D";
        protected const string QUANXIAN_EXPORT = "E";
        protected const string QUANXIAN_CONTROLOTHER = "C";
        #endregion

        #region Ctor
        public PVGongXuAdminPage()
        {
            CheJianManagement = new TCheJian();
            CheJianGongXu = new TGongXu();
        }
        #endregion

        #region Properties
        protected TCheJian CheJianManagement { get; private set; }
        protected TGongXu CheJianGongXu { get; private set; }
        #endregion
                
        #region Abstract Methods
        protected abstract string GetQuanXianValue();
        #endregion

        #region Permission
        protected bool HasPermission(GongXuQuanXianLeiXing quanXianLeiXing, string quanXianValue=null)
        {
            quanXianValue = quanXianValue ?? GetQuanXianValue();
            var hasPermission = true;

            if ((quanXianLeiXing & GongXuQuanXianLeiXing.ADD) == GongXuQuanXianLeiXing.ADD)
                hasPermission = PublicMethod.HasPermission(string.Format("|{0}{1}|", quanXianValue, QUANXIAN_ADD));
            if ((quanXianLeiXing & GongXuQuanXianLeiXing.CONTROLOTHER) == GongXuQuanXianLeiXing.CONTROLOTHER)
                hasPermission = PublicMethod.HasPermission(string.Format("|{0}{1}|", quanXianValue, QUANXIAN_CONTROLOTHER));
            if ((quanXianLeiXing & GongXuQuanXianLeiXing.DELETE) == GongXuQuanXianLeiXing.DELETE)
                hasPermission = PublicMethod.HasPermission(string.Format("|{0}{1}|", quanXianValue, QUANXIAN_DELETE));
            if ((quanXianLeiXing & GongXuQuanXianLeiXing.EXPORT) == GongXuQuanXianLeiXing.EXPORT)
                hasPermission = PublicMethod.HasPermission(string.Format("|{0}{1}|", quanXianValue, QUANXIAN_EXPORT));
            if ((quanXianLeiXing & GongXuQuanXianLeiXing.MODIFY) == GongXuQuanXianLeiXing.MODIFY)
                hasPermission = PublicMethod.HasPermission(string.Format("|{0}{1}|", quanXianValue, QUANXIAN_MODIFY));

            return hasPermission;
        }
        #endregion

        #region GridViewToExcel
        protected void GridViewToExcel(GridView gridView, string tableName, string firstColumnName = null, string firstColumnTitle = null)
        {
            string IDList = "0";
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                //Label LabVis = (Label)GVData.Rows[i].FindControl("LabVisible");
                //IDList = IDList + "," + LabVis.Text.ToString();

                IDList = IDList + "," + DataBinder.Eval(gridView.Rows[0].DataItem, "ID");
            }

            var MyTable = new Hashtable();
            if (!string.IsNullOrEmpty(firstColumnName) && !string.IsNullOrEmpty(firstColumnTitle))
            {
                MyTable[firstColumnName] = firstColumnTitle;
            }
            foreach (DataControlField column in gridView.Columns)
            {
                if (column is BoundField)
                {
                    MyTable[(column as BoundField).DataField] = column.HeaderText;
                }
            }

            var sqlBuilder = new StringBuilder();
            sqlBuilder.Append("SELECT ");
            var index = 0;
            foreach (var key in MyTable.Keys)
            {
                if (index++ > 0)
                    sqlBuilder.Append(" , ");
                sqlBuilder.Append(key);
            }
            sqlBuilder.AppendFormat(" FROM {0} WHERE ID in ({1}) ORDER BY ID DESC", tableName, IDList);

            DataToExcel.GridViewToExcel(DbHelperSQL.GetDataSet(sqlBuilder.ToString()), MyTable, "Excel报表-" + tableName);
        }
        #endregion
    }
}